package br.com.intelsys.basics.spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.NativeWebRequest;

public abstract class WebReqGeneric {

	private final NativeWebRequest webRequest;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private ISessionService iSessionService;

	public WebReqGeneric(final NativeWebRequest webRequest) {
		this(webRequest, null, null);
	}

	public WebReqGeneric(final HttpServletRequest request, final HttpServletResponse response) {
		this(null, request, response);
	}

	public WebReqGeneric(final NativeWebRequest webRequest, final HttpServletRequest request, final HttpServletResponse response) {
		this.webRequest = webRequest;
		this.request = request;
		this.response = response;
	}

	protected NativeWebRequest getWebRequest() {
		return webRequest;
	}

	protected HttpServletRequest getRequest() {
		if (request == null && webRequest != null) {
			request = (HttpServletRequest) webRequest.getNativeRequest();
		}
		return request;
	}

	protected HttpServletResponse getResponse() {
		return response;
	}

	public CookieMap getCookieMap() {
		return new CookieMap(response, request.getCookies());
	}

	public ISessionService getISessionService() {
		if (iSessionService == null) {
			iSessionService = loadISessionService();
		}
		return iSessionService;
	}

	protected abstract ISessionService loadISessionService();

}
